﻿var home = {
    current: null,
    currentWidth:null,
    currentLength: null,
    email:null,
    urlParams:[],

    init: function(){
	$.getScriptSafe('/static/settings.js', home.loadMlPack);
        trace('home : init');
        
		var spanInput = $('<span/>').addClass('input')
			.append( $('<span/>').addClass('fL') )
			.append( $('<span/>').addClass('fR') );

		$('.home dl dd div').append( spanInput.clone() );

        home.current = 0;
        home.currentWidth = 600;
        home.currentLength = $('.home .banner ul li').length;

        home.banner.structure();

	home.login.init();
	// Get query string
	//alert(queryString());
	/*
	home.queryString();
	home.isML = home.urlParams["isML"];
	alert(isML);
	*/
	home.qString();
	home.email = home.urlParams["email"];
    },
    loadMlPack : function(){
        $.getScriptSafe('/static/js/lib/ml-utils.js', home.allLoadsComplete);
    },
    allLoadsComplete : function(){
	// Post login form
	$('.bt-enter').bind('click', function() {
		var frm = $('#login');
		frm.attr("action",settings.user.loginFetch); 
		frm.submit();
	});
	// Post new user
	$('.bt-ok').bind('click', function() {
		var frm = $('#cadUser');
		frm.attr("action",settings.user.newUserFetch); 
		frm.submit();
	});
	if(home.email!=null){
		$('.email').find('input').val(home.email);
		$('.email').find('.txt-cpf').show();
	}
    },
	qString:function () {
		var e,
		a = /\+/g,
		r = /([^&=]+)=?([^&]*)/g,
		d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
		q = window.location.search.substring(1);
		while (e = r.exec(q))
			home.urlParams[d(e[1])] = d(e[2]);
	},

	login: {
		init: function() {
			
		},
		
		send: function() {
			
		}
	},

    banner: {
        _setInterval: null,
    
        structure: function(){
            $('.home .banner ul')
                .wrap($('<div/>').addClass('mask'));
               
            $('.home .banner')
                .append('<ol/>');
                
            $.each($('.home .banner ul li'), function(i, item){
                $(item).addClass('ct'+i);
            
                $('<li/>')
                    .appendTo('.home .banner ol')
                    .attr('rel', i)
                    .text(i+1)
                    .bind('click', home.banner.clickPagination);
            });
            
            $('.home .banner ol li').eq(0).trigger('click');
            
            home.banner.timaline();
        },
        
        timaline: function(){
            clearInterval(home.banner._setInterval);
            home.banner._setInterval = setInterval(function(){
                if(home.current <= home.currentLength){
                    home.current++;
                    $('.home .banner ol li').eq(home.current).trigger('click');
                    trace(home.current);
                } else{
                    home.current = 0;
                    $('.home .banner ol li').eq(home.current).trigger('click');
                }
            }, 3000);
        },
        
        clickPagination: function(e){
            var _this = $(e.currentTarget);
            var _rel = _this.attr('rel');
            
            
            $('.home .banner ol li').removeClass('on');
            _this.addClass('on');
            
            home.current = _rel;
            home.banner.animateBanner();
            
            clearInterval(home.banner._setInterval);
            home.banner.timaline();
        },
        
        animateBanner: function(){
            var _animate = home.current * home.currentWidth;
            
            $('.home .banner ul').animate({
                marginLeft: -_animate
            },{
                duration: 500,
                queue: false,
                easing: 'easeOutExpo'
            });
        }
    }
};

$(document).ready(home.init);
